package com.imay.photopicker.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.imay.capturefilter.R;
import com.imay.capturefilter.camera.CameraEngine;
import com.imay.capturefilter.utils.ICCons;
import com.imay.photopicker.ClipActivity;
import com.imay.photopicker.adapter.PhotoGridAdapter;
import com.imay.photopicker.entity.Photo;
import com.imay.photopicker.view.GridVerticalDividerItemDecoration;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import static com.imay.photopicker.PhotoPicker.EXTRA_PREVIEW_ENABLED;

/**
 * Created by donglua on 15/5/31.
 */
public class PhotoPickerFragment extends BaseFragment {

    private List<Photo> photos;

    private ArrayList<String> originalPhotos;


    private PhotoGridAdapter photoGridAdapter;

    private int column;

    public final static int DEFAULT_COLUMN_NUMBER = 3;

    private final static String EXTRA_CAMERA = "camera";
    private final static String EXTRA_COLUMN = "column";
    private final static String EXTRA_COUNT = "count";
    private final static String EXTRA_GIF = "gif";
    private final static String EXTRA_ORIGIN = "origin";

    private int mCurrentPosition = -1;

    public static PhotoPickerFragment newInstance(boolean showCamera, boolean showGif,
                                                  boolean previewEnable, int column, int maxCount, ArrayList<String> originalPhotos) {
        Bundle args = new Bundle();
        args.putBoolean(EXTRA_CAMERA, showCamera);
        args.putBoolean(EXTRA_GIF, showGif);
        args.putBoolean(EXTRA_PREVIEW_ENABLED, previewEnable);
        args.putInt(EXTRA_COLUMN, column);
        args.putInt(EXTRA_COUNT, maxCount);
        args.putStringArrayList(EXTRA_ORIGIN, originalPhotos);
        PhotoPickerFragment fragment = new PhotoPickerFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.picker_fragment_photo_picker;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setRetainInstance(true);


        photos = new ArrayList<>();
        originalPhotos = getArguments().getStringArrayList(EXTRA_ORIGIN);

        column = getArguments().getInt(EXTRA_COLUMN, DEFAULT_COLUMN_NUMBER);

        photoGridAdapter = new PhotoGridAdapter(getActivity(),  photos, originalPhotos, column);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        final RecyclerView mRecyclerView = $(R.id.rv_photos);
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(photoGridAdapter);
        //设置Item增加、移除动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //添加分割线

        mRecyclerView.addItemDecoration(new GridVerticalDividerItemDecoration.Builder(getActivity(), 1)
                .colorResId(R.color.divider_ff)
                .sizeResId(R.dimen.picker_item_photo_rule)
                .build());

        mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())
                .colorResId(R.color.divider_ff)
                .sizeResId(R.dimen.picker_item_photo_rule1)
                .build());

        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (position == 0) {
                    if (itemClickListener != null) {
                        if (CameraEngine.isOpen) {
                            CameraEngine.releaseCamera();
                        }
                        itemClickListener.onItemClick(position, null);
                    }
                } else {
                    Photo photo = (Photo) adapter.getData().get(position);
                    Intent intent = getActivity().getIntent();
                    intent.putExtra(ICCons.KEY_FILE_PATH, photo.getPath());
                    getActivity().setResult(Activity.RESULT_OK, intent);
                    getActivity().finish();
                    //ClipActivity.startActivity(getActivity(), ClipActivity.CLIP_REQUEST_CODE_1, photo.getPath(), null);
                }
            }
        });
    }

    public PhotoItemClickListener getItemClickListener() {
        return itemClickListener;
    }

    public void setItemClickListener(PhotoItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void setPhotos(List<Photo> photos) {
        this.photos.clear();
        this.photos.addAll(photos);
        this.photos.add(0, new Photo(-1, "", true));
        photoGridAdapter.notifyDataSetChanged();
    }

    public void notifyChanged(int i) {
        photoGridAdapter.notifyItemChanged(i);
    }

    public interface PhotoItemClickListener {
        void onItemClick(int position, String path);
    }

    private PhotoItemClickListener itemClickListener;
}
